package com.ruoyi.system.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.mapper.King.KingCangMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.KingBdOrganizationMapper;
import com.ruoyi.system.domain.KingBdOrganization;
import com.ruoyi.system.service.IKingBdOrganizationService;

/**
 * 组织机构Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-28
 */
@Service
public class KingBdOrganizationServiceImpl implements IKingBdOrganizationService 
{
    @Autowired
    private KingBdOrganizationMapper kingBdOrganizationMapper;

    @Autowired
    private KingCangMapper kingCangMapper;

    /**
     * 查询组织机构
     * 
     * @param id 组织机构ID
     * @return 组织机构
     */
    @Override
    public KingBdOrganization selectKingBdOrganizationById(Long id)
    {
        return kingBdOrganizationMapper.selectKingBdOrganizationById(id);
    }

    /**
     * 查询组织机构列表
     * 
     * @param kingBdOrganization 组织机构
     * @return 组织机构
     */
    @Override
    public List<KingBdOrganization> selectKingBdOrganizationList(KingBdOrganization kingBdOrganization)
    {
        return kingBdOrganizationMapper.selectKingBdOrganizationList(kingBdOrganization);
    }

    /**
     * 新增组织机构
     * 
     * @param kingBdOrganization 组织机构
     * @return 结果
     */
    @Override
    public int insertKingBdOrganization(KingBdOrganization kingBdOrganization)
    {
        return kingBdOrganizationMapper.insertKingBdOrganization(kingBdOrganization);
    }

    /**
     * 修改组织机构
     * 
     * @param kingBdOrganization 组织机构
     * @return 结果
     */
    @Override
    public int updateKingBdOrganization(KingBdOrganization kingBdOrganization)
    {
        return kingBdOrganizationMapper.updateKingBdOrganization(kingBdOrganization);
    }

    /**
     * 批量删除组织机构
     * 
     * @param ids 需要删除的组织机构ID
     * @return 结果
     */
    @Override
    public int deleteKingBdOrganizationByIds(Long[] ids)
    {
        return kingBdOrganizationMapper.deleteKingBdOrganizationByIds(ids);
    }

    /**
     * 删除组织机构信息
     * 
     * @param id 组织机构ID
     * @return 结果
     */
    @Override
    public int deleteKingBdOrganizationById(Long id)
    {
        return kingBdOrganizationMapper.deleteKingBdOrganizationById(id);
    }

    @Override
    public int getNum(String fId){ return  kingBdOrganizationMapper.getNum(fId);}

    @Override
    public List<Map<String, String>> getALL(){ return  kingBdOrganizationMapper.getALL();}

    @Override
    public String fIdToCode(String fId){
        return kingBdOrganizationMapper.fIdToCode(fId);
    }

    @Override
    public String fCodeToId(String fCode){
        return kingBdOrganizationMapper.fCodeToId(fCode);
    }

    @Override
    public  List<Map<String, String>> getALLnot(){ return kingBdOrganizationMapper.getALLnot();}

    @Override
    public String getOrganDepartList(){
        List<Map<String, String>> list = getALLnot();
        List<Map<String, Object>> mapList = new LinkedList<>();
        if(list.size()>0){
            for(Map<String, String> map:list){
                Map<String, Object> mapObj = new LinkedHashMap<>();
                List<Map<String, String>> list1 = kingCangMapper.getDepartMentList(map.get("laber"));
                List<Map<String, String>> lis = new ArrayList<>();
                if(list1.size()>0){
                    for(Map<String, String> map1 :list1){
                        Map<String, String> map2 = new LinkedHashMap<>();
                        map2.putAll(map1);
                        map2.put("pid",fIdToCode(map.get("laber")));
                        lis.add(map2);
                    }
                }
                mapObj.put("childs",lis);
                mapObj.put("id",fIdToCode(map.get("laber")));
                mapObj.put("name",map.get("value"));
                mapObj.put("pid","0");
                mapList.add(mapObj);
            }
        }
        return JSONObject.toJSONString(mapList);
    }
}
